package com.example.fangkb;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
            private long lastPressTime  =  0;
            private Button btnStartB;
            private final String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate");

        btnStartB=findViewById(R.id.btn_start_b);
        btnStartB.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent =new Intent();
                intent.setClass(MainActivity.this,ActivityB.class);
                startActivity(intent);
            }

        });
    }
    @Override
    public void onBackPressed(){
        long currentPressTime=System.currentTimeMillis();
        if(currentPressTime-lastPressTime>2000){
            lastPressTime = currentPressTime;
            Toast.makeText(MainActivity.this,"再按一次退出",Toast.LENGTH_SHORT).show();
        }else{
            MainActivity.super.onBackPressed();
        }
        Log.e(TAG,"currentPressTime = "+currentPressTime);

        super.onBackPressed();
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.e(TAG, "onCreate" );
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.e(TAG, "onPause");
    }
    @Override
    protected void onStop(){
        super.onStop();
        Log.e(TAG, "onStop");
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.e(TAG, "onDestroy");

    }
}
